package com.wj.console.common.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.base.util.BaseAction;
import com.wj.console.constant.Constant;

/**
 * 资源下载
 * 
 * @author wj
 * @date 2016年4月22日 上午9:47:00
 * 
 */
@Controller
@RequestMapping(value = "/resourceDownload")
public class ResourceDownloadController extends BaseAction {

	@Autowired
	private Constant constant;

	private final static Logger logger = Logger.getLogger(ResourceDownloadController.class);

	/**
	 * 图片下载
	 * 
	 * @param request
	 * @param response
	 * @author ZhangYuhang
	 * @date 2016年4月22日 上午9:49:20
	 */
	@RequestMapping(value = "/img")
	public void imgDownload(HttpServletRequest request, HttpServletResponse response, String imgPath) {
		if(StringUtils.isNotBlank(imgPath)){
			
			String img = constant.getFileUploadPath() + File.separator + imgPath;
			try {
				InputStream in = new FileInputStream(img);
				OutputStream out = response.getOutputStream();
				byte[] b = new byte[1024];
				int n;
				while ((n = in.read(b)) != -1) {
					out.write(b, 0, n);
				}
				out.flush();
				out.close();
				in.close();
			} catch (Exception e) {
				logger.error("图片下载", e);
			}
		}

	}
}
